﻿using DotNetCommon.Data;
using DotNetCommon.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace NetAssistant
{
    public partial class TcpServerFrom : Form
    {
        private static string mqttIP = "10.168.1.102";
        private static int mqttPort = 12803;
        private static string mqttUser = "caoejiang_mqtt";
        private static string mqttPwd = "caoejiang";
        private static string mqttTopic = "twenty_one_gate_data";
        private static string clientId = "caoejiang_21";

        private MqttClientManage? mqttClientManage = null;

        private Thread receiveThread;
        // TCP服务端监听
        private TcpListener tcpServer = null;
        //是否启动 连接了
        private bool isMonitor = false;
        //创建httpTcp内容
        private HttpReturnValue httpReturnValue = new HttpReturnValue();
        // 客户端连接初始化
        public static List<string> dictInfo = new List<string>();
        //连接的客户端列表
        public static List<TcpClient> tcpClients = new List<TcpClient>();
        //连接的客户端监听列表
        public static List<Thread> tcpClientThreads = new List<Thread>();
        //当前时间
        private long lastTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
        //采样频率
        private long frequency = 1000;
        //采集间隔
        private long intervalTime = 1000;//(毫秒)

        public TcpServerFrom()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            mqttClientManage = new MqttClientManage();
            if (mqttClientManage != null)
            {
                mqttClientManage.ConnectAsync(mqttIP, mqttPort, mqttUser, mqttPwd, clientId);
                mqttClientManage.SubscribeAsync(mqttTopic);
            }
        }


        public static List<int> indexOfByteArray(byte[] data, byte[] pattern)
        {
            List<int> matchedPos = new List<int>();

            if (data.Length == 0 || data.Length < pattern.Length) return matchedPos;

            int end = data.Length - pattern.Length;
            bool matched = false;

            for (int i = 0; i <= end; i++)
            {
                for (int j = 0; j < pattern.Length || !(matched = (j == pattern.Length)); j++)
                {
                    if (data[i + j] != pattern[j]) break;
                }
                if (matched)
                {
                    matched = false;
                    matchedPos.Add(i);
                }
            }
            return matchedPos;
        }

        //把数据信息转成 选定的编码格式数据 再 调用添加数据 方法
        public async Task AddDataAsync(string ClientIPtxt, byte[] srcData)
        {
            switch (CodingType)
            {
                case 0:
                    //U3H-16进制显示
                    byte[] headerArray = new byte[4] {165,165,165,165};
                    List<int> matchedPos = indexOfByteArray(srcData, headerArray);
                    headerArray = null;
                    if (matchedPos.Count <= 0) { break; }
                    for(int index = 0; index < matchedPos.Count - 1; index++)
                    {
                        int begPos = matchedPos[index];
                        int endPos = matchedPos[index + 1];
                        byte[] data = new byte[endPos-begPos];
                        Array.Copy(srcData, begPos, data, 0, endPos - begPos);

                        //协议头: 4 字节，固定0xA5A5A5A5
                        //byte[] protocolHeaderArray = new byte[4];
                        //Array.Copy(data, 0, protocolHeaderArray, 0, 4);
                        //StringBuilder sb = new StringBuilder();
                        //for (int i = 0; i < protocolHeaderArray.Length; i++)
                        //{
                        //    sb.AppendFormat("{0:x2}", protocolHeaderArray[i]);
                        //}
                        //protocolHeaderArray = null;

                        string jsonString = "";
                        //设备ID：4 字节，为设备ID 的末4 位
                        byte[] deviceIdArray = new byte[4];
                        Array.Copy(data, 4, deviceIdArray, 0, 4);
                        string deviceId = new ASCIIEncoding().GetString(deviceIdArray);
                        string deviceName = "qc_raem1" + deviceId;
                        string id = deviceId.Substring(deviceId.Length - 3, 3);
                        deviceIdArray = null;

                        //数据类型：4 字节， 0x0000000B 为参数数据  0x0000000A 为波形数据  
                        byte[] dataTypeArray = new byte[4];
                        Array.Copy(data, 8, dataTypeArray, 0, 4);
                        int dataType = BitConverter.ToInt32(dataTypeArray);
                        dataTypeArray = null;

                        //数据类型： 参数
                        if (dataType == 11)
                        {
                            DeviceParamData deviceParamData = new DeviceParamData();
                            deviceParamData.deviceType = 11;
                            deviceParamData.name = deviceName;
                            deviceParamData.deviceId = Convert.ToUInt16(id);
                            deviceParamData.dataType = dataType;

                            //数据长度：4 字节，表示数据内容的字节长度
                            //byte[] dataLengthArray = new byte[4];
                            //Array.Copy(data, 12, dataLengthArray, 0, 4);
                            //uint dataLength = BitConverter.ToUInt32(dataLengthArray);
                            //dataLengthArray = null;

                            //数据内容：参数数据的内容
                            //Version ， 协议版本 4 字节数据
                            //byte[] versionArray = new byte[4];
                            //Array.Copy(data, 16, versionArray, 0, 4);

                            //到达时间的秒数 unsigned int 类型，4 位
                            byte[] secondsArray = new byte[4];
                            Array.Copy(data, 20, secondsArray, 0, 4);
                            int seconds = BitConverter.ToInt32(secondsArray);
                            deviceParamData.timestamp = seconds;
                            secondsArray = null;

                            //到达时间的纳秒数 unsigned int 类型，4 位
                            byte[] nanoSecondsArray = new byte[4];
                            Array.Copy(data, 24, nanoSecondsArray, 0, 4);
                            int nanoSeconds = BitConverter.ToInt32(nanoSecondsArray);
                            deviceParamData.nanoSeconds = nanoSeconds;
                            nanoSecondsArray = null;

                            //AMP，幅度，单位为dB double 类型，8 位
                            byte[] ampArray = new byte[8];
                            Array.Copy(data, 28, ampArray, 0, 8);
                            double amp = BitConverter.ToDouble(ampArray);
                            deviceParamData.value = amp;
                            ampArray = null;

                            //Power，能量，单位为 KpJ double 类型，8 位
                            byte[] powerArray = new byte[8];
                            Array.Copy(data, 36, powerArray, 0, 8);
                            double power = BitConverter.ToDouble(powerArray);
                            deviceParamData.value1 = power;
                            powerArray = null;

                            //RMS，有效电压，单位为mV double 类型，8 位
                            byte[] rmsArray = new byte[8];
                            Array.Copy(data, 44, rmsArray, 0, 8);
                            double rms = BitConverter.ToDouble(rmsArray);
                            deviceParamData.value2 = rms;
                            rmsArray = null;

                            //ASL，平均信号电平值，单位为 dB double 类型，8 位
                            byte[] aslArray = new byte[8];
                            Array.Copy(data, 52, aslArray, 0, 8);
                            double asl = BitConverter.ToDouble(aslArray);
                            deviceParamData.value3 = asl;
                            aslArray = null;

                            //上升时间，单位为 us unsigned int 类型，4 位
                            byte[] rtArray = new byte[4];
                            Array.Copy(data, 60, rtArray, 0, 4);
                            deviceParamData.value4 = BitConverter.ToUInt32(rtArray);
                            rtArray = null;

                            //上升计数，单位为个 unsigned int 类型，4 位
                            byte[] upArray = new byte[4];
                            Array.Copy(data, 64, upArray, 0, 4);
                            deviceParamData.value5 = BitConverter.ToUInt32(upArray);
                            upArray = null;

                            //采样波形点数，单位为个 us unsigned int 类型，4 位 
                            byte[] wpArray = new byte[4];
                            Array.Copy(data, 68, wpArray, 0, 4);
                            deviceParamData.value6 = BitConverter.ToUInt32(wpArray);
                            wpArray = null;

                            //持续时间，单位为 us unsigned int 类型，4 位 
                            byte[] dtArray = new byte[4];
                            Array.Copy(data, 72, dtArray, 0, 4);
                            deviceParamData.value7 = BitConverter.ToUInt32(dtArray);
                            dtArray = null;

                            //振铃计数，单位为个 unsigned int 类型，4 位
                            byte[] rcArray = new byte[4];
                            Array.Copy(data, 76, rcArray, 0, 4);
                            deviceParamData.value8 = BitConverter.ToUInt32(rcArray);
                            rcArray = null;

                            //峰值频率，单位 KHz unsigned int 类型，4 位
                            byte[] pfArray = new byte[4];
                            Array.Copy(data, 80, pfArray, 0, 4);
                            deviceParamData.value9 = BitConverter.ToUInt32(pfArray);
                            pfArray = null;

                            //质心频率，单位 KHz unsigned int 类型，4 位
                            byte[] cfArray = new byte[4];
                            Array.Copy(data, 84, cfArray, 0, 4);
                            deviceParamData.value10 = BitConverter.ToUInt32(cfArray);
                            cfArray = null;

                            //局部功率谱 1 占比 unsigned int 类型，4 位
                            byte[] lpArray1 = new byte[4];
                            Array.Copy(data, 88, lpArray1, 0, 4);
                            deviceParamData.value11 = BitConverter.ToUInt32(lpArray1);
                            lpArray1 = null;

                            //局部功率谱 2 占比 unsigned int 类型，4 位
                            byte[] lpArray2 = new byte[4];
                            Array.Copy(data, 92, lpArray2, 0, 4);
                            deviceParamData.value12 = BitConverter.ToUInt32(lpArray2);
                            lpArray2 = null;

                            //局部功率谱 3 占比 unsigned int 类型，4 位
                            byte[] lpArray3 = new byte[4];
                            Array.Copy(data, 96, lpArray3, 0, 4);
                            deviceParamData.value13 = BitConverter.ToUInt32(lpArray3);
                            lpArray3 = null;

                            //局部功率谱 4 占比 unsigned int 类型，4 位
                            byte[] lpArray4 = new byte[4];
                            Array.Copy(data, 100, lpArray4, 0, 4);
                            deviceParamData.value14 = BitConverter.ToUInt32(lpArray4);
                            lpArray4 = null;

                            //局部功率谱 5 占比 unsigned int 类型，4 位
                            byte[] lpArray5 = new byte[4];
                            Array.Copy(data, 104, lpArray5, 0, 4);
                            deviceParamData.value15 = BitConverter.ToUInt32(lpArray5);
                            lpArray5 = null;
                            jsonString = JsonConvert.SerializeObject(deviceParamData);
                            await U3HMqttDataPublic(jsonString);
                        }
                        //数据类型： 波形
                        if (dataType == 10)
                        {
                            DeviceWaveData deviceWaveData = new DeviceWaveData();
                            deviceWaveData.deviceType = 11;
                            deviceWaveData.name = deviceName;
                            deviceWaveData.deviceId = Convert.ToUInt16(id);
                            deviceWaveData.dataType = dataType;

                            //数据长度：4 字节，表示数据内容的字节长度
                            byte[] dataLengthArray1 = new byte[4];
                            Array.Copy(data, 12, dataLengthArray1, 0, 4);
                            uint dataLength1 = BitConverter.ToUInt32(dataLengthArray1);
                            dataLengthArray1 = null;

                            //到达时间的秒数 unsigned int 类型，4 位
                            byte[] secondsArray1 = new byte[4];
                            Array.Copy(data, 16, secondsArray1, 0, 4);
                            int seconds1 = BitConverter.ToInt32(secondsArray1);
                            deviceWaveData.timestamp = seconds1;
                            secondsArray1 = null;

                            //到达时间的纳秒数 unsigned int 类型，4 位
                            byte[] nanoSecondsArray1 = new byte[4];
                            Array.Copy(data, 20, nanoSecondsArray1, 0, 4);
                            int nanoSeconds1 = BitConverter.ToInt32(nanoSecondsArray1);
                            deviceWaveData.nanoSeconds = nanoSeconds1;
                            nanoSecondsArray1 = null;

                            //Version ， 协议版本 4 字节数据
                            //byte[] versionArray1 = new byte[4];
                            //Array.Copy(data, 24, versionArray1, 0, 4);
                            //versionArray1 = null;

                            //PreSample_Points，前采点数，单位为个，单位为个 unsigned int 类型 ， 4 字节数据
                            byte[] prePointsArray = new byte[4];
                            Array.Copy(data, 28, prePointsArray, 0, 4);
                            uint prePoints = BitConverter.ToUInt32(prePointsArray);
                            deviceWaveData.value = prePoints;
                            prePointsArray = null;

                            //Points，波形点数，单位为个 unsigned int 类型 ， 4 字节数据
                            byte[] pointsArray = new byte[4];
                            Array.Copy(data, 32, pointsArray, 0, 4);
                            uint points = BitConverter.ToUInt32(pointsArray);
                            deviceWaveData.value1 = points;
                            pointsArray = null;

                            //Speed， 采样速率 ，单位为 K/S unsigned int 类型 ， 4 字节数据
                            byte[] speedArray = new byte[4];
                            Array.Copy(data, 36, speedArray, 0, 4);
                            uint speed = BitConverter.ToUInt32(speedArray);
                            deviceWaveData.value2 = speed;
                            speedArray = null;

                            //Gain， 前放增益 ， 单位为倍 unsigned int 类型 ， 4 字节数据
                            byte[] gaindArray = new byte[4];
                            Array.Copy(data, 40, gaindArray, 0, 4);
                            uint gain = BitConverter.ToUInt32(gaindArray);
                            deviceWaveData.value3 = gain;
                            gaindArray = null;

                            //Enlarge， 电路放大倍数 ， 单位为倍 Double 类型， 8 字节数据
                            byte[] enlargeArray = new byte[8];
                            Array.Copy(data, 44, enlargeArray, 0, 8);
                            double enlarge = BitConverter.ToDouble(enlargeArray);
                            deviceWaveData.value4 = enlarge;
                            enlargeArray = null;

                            //波形数据 n 一个波形数据为 2 字节，即此处有 2n 个波形点数的数据，总大小为 2n 字节
                            byte[] waveDataArray = new byte[dataLength1 - 36];
                            Array.Copy(data, 52, waveDataArray, 0, dataLength1 - 36);
                            double[] shortArray = new double[waveDataArray.Length / 2];
                            for (int i = 0; i < waveDataArray.Length; i += 2)
                            {
                                double dB = 0;
                                int wave = BitConverter.ToInt16(waveDataArray, i);
                                double uV = (wave * 2.048) / 6;
                                if (uV > 0)
                                {
                                    dB = 20 * Math.Log10(uV);
                                }
                                else if (uV < 0)
                                {
                                    dB = -20 * Math.Log10(Math.Abs(uV));
                                }
                                shortArray[i / 2] = dB;
                            }
                            deviceWaveData.value5 = shortArray;
                            jsonString = JsonConvert.SerializeObject(deviceWaveData);
                            await U3HMqttDataPublic(jsonString);
                            shortArray = null;
                            waveDataArray = null;
                        }
                        AddContent(ClientIPtxt, jsonString);
                        data = null;
                    }
                    matchedPos.Clear();
                    matchedPos = null;
                    break;
                case 1:
                    //TCP-ASCII码显示
                    string reciveMessage1 = BitConverter.ToString(srcData);
                    //格式为设备ID + 幅度AMP单位为dB + 平均信号电平ASL单位为dB + 能量Power单位为KpJ + 有效值电压RMS单位为mV + 时间戳
                    if (reciveMessage1 != null && reciveMessage1.Length > 0)
                    {
                        ReciveWaveDataCFBG(srcData);
                        //string[] values = reciveMessage1.Split(',');
                        //await TCPMqttDataPublic(values);
                        AddContent(ClientIPtxt, reciveMessage1);
                        //await SaveDataToMysql(values, DateTimeOffset.Now.ToUnixTimeMilliseconds(), 11);
                    }

                    break;
                case 2:
                    //GB2312显示
                    //AddContent(ClientIPtxt, Encoding.GetEncoding("GB2312").GetString(srcData));
                    //TCPV2-ASCII码显示
                    //string reciveMessage2 = new ASCIIEncoding().GetString(srcData);
                    //格式为设备ID + 幅度AMP单位为dB + 平均信号电平ASL单位为dB + 能量Power单位为KpJ + 有效值电压RMS单位为mV + 时间戳
                    //if (reciveMessage2 != null && reciveMessage2.Length > 0)
                    //{
                        //string[] values = reciveMessage2.Split(',');
                        //await TCPv2MqttDataPublic(values);
                        //AddContent(ClientIPtxt, reciveMessage2);
                        //await SaveDataToMysql(values, DateTimeOffset.Now.ToUnixTimeMilliseconds(), 11);
                    //}
                    break; ;
                case 3:
                    //UTF8显示
                    //AddContent(ClientIPtxt, new UTF8Encoding().GetString(srcData));
                    //MQTT-ASCII码显示
                    //string reciveMessage3 = new ASCIIEncoding().GetString(srcData);
                    //格式为设备ID + 幅度AMP单位为dB + 平均信号电平ASL单位为dB + 能量Power单位为KpJ + 有效值电压RMS单位为mV + 时间戳
                    //if (reciveMessage3 != null && reciveMessage3.Length > 0)
                    //{
                        //string[] values = reciveMessage3.Split(',');
                        //await TCPv2MqttDataPublic(values);
                        //AddContent(ClientIPtxt, reciveMessage3);
                        //await SaveDataToMysql(values, DateTimeOffset.Now.ToUnixTimeMilliseconds(), 11);
                    //}
                    break;
            }
        }

        private void ReciveWaveDataCFBG(byte[] revData)
        {
            long nowTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            if ((nowTime - this.lastTime) < this.intervalTime){
                return;
            }
            this.lastTime = nowTime;
            //系统标识 string 4Byte CFBG
            byte[] sysSignArray = new byte[4];
            Array.Copy(revData, 0, sysSignArray, 0, 4);
            string sysSign = new ASCIIEncoding().GetString(sysSignArray);
            if ("CFBG".EqualsAnyIgnoreCase(sysSign))
            {
                //数据总长 第2部分数据总字节数，帧数据长度+2字节CRC16验证 int 4Byte 
                byte[] lengthArray = new byte[4];
                Array.Copy(revData, 4, lengthArray, 0, 4);
                Array.Reverse(lengthArray);
                int length = BitConverter.ToInt32(lengthArray);
                lengthArray = null;
                //if (length == 0){
                //    return;
                //}
                //帧数	第2部分帧数, 等于设备采集频率 int 4Byte
                byte[] frequenceArray = new byte[4];
                Array.Copy(revData, 8, frequenceArray, 0, 4);
                Array.Reverse(frequenceArray);
                int frequence = BitConverter.ToInt32(frequenceArray);
                frequenceArray = null;
                //每帧数据字符串首位相接，可通过标识头“CFBG”或第1部分的帧数进行切分；整部分最后2字节为CRC16验证码，如验证成功服务端需向客户端发送2字节“01”否则发送“02”，之后客户端会重发该条数据。
                byte[] tmpArray = new byte[revData.Length - 12];
                Array.Copy(revData, 12, tmpArray, 0, revData.Length -12);
                List<int> matchedPos = indexOfByteArray(tmpArray, sysSignArray);
                if (matchedPos.Count <= 0) { return; }
                for (int index = 0; index < matchedPos.Count - 1; index++)
                {
                    int begPos = matchedPos[index];
                    int endPos = matchedPos[index + 1];
                    if(endPos-begPos <= 12)
                    {
                        continue;
                    }
                    byte[] tmpDataArray = new byte[endPos - begPos];
                    Array.Copy(tmpArray, begPos, tmpDataArray, 0, endPos - begPos);

                    //系统标识 string 4Byte CFBG
                    sysSignArray = new byte[4];
                    Array.Copy(tmpDataArray, 0, sysSignArray, 0, 4);
                    sysSign = new ASCIIEncoding().GetString(sysSignArray);
                    //2Byte 16 bit的unsigned short： bit 0~15：年
                    byte[] yearArray = new byte[2];
                    Array.Copy(tmpDataArray, 4, yearArray, 0, 2);
                    Array.Reverse(yearArray);
                    uint year = BitConverter.ToUInt16(yearArray);
                    yearArray = null;
                    //2Byte 16 bit的unsigned short： bit 0~7：日 bit 8~15：月
                    char[] monthArray = new char[1];
                    Array.Copy(tmpDataArray, 6, monthArray, 0, 1);
                    //Array.Reverse(monthArray);
                    char month = monthArray[0];
                    monthArray = null;

                    char[] dayArray = new char[1];
                    Array.Copy(tmpDataArray, 7, dayArray, 0, 1);
                    //Array.Reverse(dayArray);
                    char day = dayArray[0];
                    dayArray = null;
                    //2Byte 16 bit的unsigned short：bit 0~7：分 bit 8~15：时
                    char[] hourArray = new char[1];
                    Array.Copy(tmpDataArray, 8, hourArray, 0, 1);
                    //Array.Reverse(hourArray);
                    char hour = hourArray[0];
                    hourArray = null;

                    char[] minArray = new char[1];
                    Array.Copy(tmpDataArray, 9, minArray, 0, 1);
                    //Array.Reverse(minArray);
                    char min = minArray[0];
                    minArray = null;
                    //4Byte 32 bit的float：秒.毫秒
                    byte[] secArray = new byte[8];
                    Array.Copy(tmpDataArray, 10, secArray, 4, 4);
                    Array.Reverse(secArray);
                    double sec = BitConverter.ToDouble(secArray);
                    secArray = null;
                    //2Byte unsigned short 接下来的数据长度，单位为字节
                    byte[] lengthArray1 = new byte[2];
                    Array.Copy(revData, 14, lengthArray1, 0, 2);
                    Array.Reverse(lengthArray1);
                    uint length1 = BitConverter.ToUInt16(lengthArray1);
                    lengthArray1 = null;
                    //2Byte unsigned short 全局告警 每bit代表一种告警，bit位为1告警，为0正常。 bit 0：设备与软件连接中断 bit 1：传感器故障 bit 2：设备温度告警
                    //告警可以累加，所有的告警状态不保持，即故障消除，告警消失。当没有启动采集时，该状态bit位0，当启动后，网络通信中断，该状态为1（设备与软件连接中断）
                    byte[] alarmArray = new byte[2];
                    Array.Copy(revData, 16, alarmArray, 0, 2);
                    //Array.Reverse(alarmArray);
                    //1Byte uchar 采集状态 1：正常采集 0：停止采集
                    char[] collectArray = new char[1];
                    Array.Copy(tmpDataArray, 18, collectArray, 0, 1);
                    //Array.Reverse(collectArray);
                    char collect = collectArray[0];
                    collectArray = null;

                    //1Byte uchar 设备通道数 通道范围：1~N
                    char[] channelArray = new char[1];
                    Array.Copy(tmpDataArray, 19, channelArray, 0, 1);
                    //Array.Reverse(channelArray);
                    char channelCut = channelArray[0];
                    channelArray = null;

                    //1Byte uchar 通道0 传感器数量 bit 0~7：通道0传感器数量，此为虚拟通道，组合光栅类型为自定义
                    char[] channelSensorCntArray0 = new char[1];
                    Array.Copy(tmpDataArray, 20, channelSensorCntArray0, 0, 1);
                    //Array.Reverse(channelSensorCntArray0);
                    char channelSensorCnt0 = channelSensorCntArray0[0];
                    channelSensorCntArray0 = null;

                    //2Byte ushort 通道1~2 传感器数量 bit 0~7：通道1传感器数量 bit 8~15：通道2传感器数量
                    char[] channelSensorCntArray1 = new char[1];
                    Array.Copy(tmpDataArray, 21, channelSensorCntArray1, 0, 1);
                    //Array.Reverse(channelSensorCntArray1);
                    char channelSensorCnt1 = channelSensorCntArray1[0];
                    channelSensorCntArray1 = null;

                    char[] channelSensorCntArray2 = new char[1];
                    Array.Copy(tmpDataArray, 22, channelSensorCntArray2, 0, 1);
                    //Array.Reverse(channelSensorCntArray2);
                    char channelSensorCnt2 = channelSensorCntArray2[0];
                    channelSensorCntArray2 = null;

                    //2Byte ushort 通道3~4 传感器数量 bit 0~7：通道3传感器数量 bit 8~15：通道4传感器数量
                    char[] channelSensorCntArray3 = new char[1];
                    Array.Copy(tmpDataArray, 23, channelSensorCntArray3, 0, 1);
                    //Array.Reverse(channelSensorCntArray3);
                    char channelSensorCnt3 = channelSensorCntArray3[0];
                    channelSensorCntArray3 = null;

                    char[] channelSensorCntArray4 = new char[1];
                    Array.Copy(tmpDataArray, 24, channelSensorCntArray4, 0, 1);
                    //Array.Reverse(channelSensorCntArray4);
                    char channelSensorCnt4 = channelSensorCntArray4[0];
                    channelSensorCntArray4 = null;

                    //2Byte ushort 通道5~6 传感器数量 bit 0~7：通道5传感器数量 bit 8~15：通道6传感器数量
                    char[] channelSensorCntArray5 = new char[1];
                    Array.Copy(tmpDataArray, 25, channelSensorCntArray5, 0, 1);
                    //Array.Reverse(channelSensorCntArray5);
                    char channelSensorCnt5 = channelSensorCntArray5[0];
                    channelSensorCntArray5 = null;

                    char[] channelSensorCntArray6 = new char[1];
                    Array.Copy(tmpDataArray, 26, channelSensorCntArray6, 0, 1);
                    //Array.Reverse(channelSensorCntArray6);
                    char channelSensorCnt6 = channelSensorCntArray6[0];
                    channelSensorCntArray6 = null;

                    //2Byte ushort 通道7~8 传感器数量 bit 0~7：通道7传感器数量 bit 8~15：通道8传感器数量
                    char[] channelSensorCntArray7 = new char[1];
                    Array.Copy(tmpDataArray, 27, channelSensorCntArray7, 0, 1);
                    //Array.Reverse(channelSensorCntArray7);
                    char channelSensorCnt7 = channelSensorCntArray7[0];
                    channelSensorCntArray7 = null;

                    char[] channelSensorCntArray8 = new char[1];
                    Array.Copy(tmpDataArray, 28, channelSensorCntArray8, 0, 1);
                    //Array.Reverse(channelSensorCntArray8);
                    char channelSensorCnt8 = channelSensorCntArray8[0];
                    channelSensorCntArray8 = null;

                    ////2Byte ushort 通道9~10 传感器数量 bit 0~7：通道9传感器数量 bit 8~15：通道10传感器数量
                    //char[] channelSensorCntArray9 = new char[1];
                    //Array.Copy(tmpDataArray, 29, channelSensorCntArray9, 0, 1);
                    ////Array.Reverse(channelSensorCntArray9);
                    //char channelSensorCnt9 = channelSensorCntArray9[0];
                    //channelSensorCntArray9 = null;

                    //char[] channelSensorCntArray10 = new char[1];
                    //Array.Copy(tmpDataArray, 30, channelSensorCntArray10, 0, 1);
                    ////Array.Reverse(channelSensorCntArray10);
                    //char channelSensorCnt10 = channelSensorCntArray10[0];
                    //channelSensorCntArray10 = null;

                    ////2Byte ushort 通道11~12 传感器数量 bit 0~7：通道11传感器数量 bit 8~15：通道12传感器数量
                    //char[] channelSensorCntArray11 = new char[1];
                    //Array.Copy(tmpDataArray, 31, channelSensorCntArray11, 0, 1);
                    ////Array.Reverse(channelSensorCntArray11);
                    //char channelSensorCnt11 = channelSensorCntArray11[0];
                    //channelSensorCntArray11 = null;

                    //char[] channelSensorCntArray12 = new char[1];
                    //Array.Copy(tmpDataArray, 32, channelSensorCntArray12, 0, 1);
                    ////Array.Reverse(channelSensorCntArray12);
                    //char channelSensorCnt12 = channelSensorCntArray12[0];
                    //channelSensorCntArray12 = null;

                    ////2Byte ushort 通道13~14 传感器数量 bit 0~7：通道13传感器数量 bit 8~15：通道14传感器数量
                    //char[] channelSensorCntArray13 = new char[1];
                    //Array.Copy(tmpDataArray, 33, channelSensorCntArray13, 0, 1);
                    ////Array.Reverse(channelSensorCntArray13);
                    //char channelSensorCnt13 = channelSensorCntArray13[0];
                    //channelSensorCntArray13 = null;

                    //char[] channelSensorCntArray14 = new char[1];
                    //Array.Copy(tmpDataArray, 34, channelSensorCntArray14, 0, 1);
                    ////Array.Reverse(channelSensorCntArray14);
                    //char channelSensorCnt14 = channelSensorCntArray14[0];
                    //channelSensorCntArray14 = null;

                    ////2Byte ushort 通道15~16 传感器数量 bit 0~7：通道15传感器数量 bit 8~15：通道16传感器数量
                    //char[] channelSensorCntArray15 = new char[1];
                    //Array.Copy(tmpDataArray, 35, channelSensorCntArray15, 0, 1);
                    ////Array.Reverse(channelSensorCntArray15);
                    //char channelSensorCnt15 = channelSensorCntArray15[0];
                    //channelSensorCntArray15 = null;

                    //char[] channelSensorCntArray16 = new char[1];
                    //Array.Copy(tmpDataArray, 36, channelSensorCntArray16, 0, 1);
                    ////Array.Reverse(channelSensorCntArray16);
                    //char channelSensorCnt16 = channelSensorCntArray16[0];
                    //channelSensorCntArray16 = null;
                    int beginIndex = 29;
                    for (int channelId = 0; channelId < channelCut; channelId++)
                    {
                        switch (channelId)
                        {
                            case 0:
                                for (int sensorId = 0; sensorId < channelSensorCnt0; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    //Array.Reverse(sensorTypeArray);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            case 1:
                                for (int sensorId = 0; sensorId < channelSensorCnt1; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    //Array.Reverse(sensorTypeArray);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            case 2:
                                for (int sensorId = 0; sensorId < channelSensorCnt2; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    //Array.Reverse(sensorTypeArray);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            case 3:
                                for (int sensorId = 0; sensorId < channelSensorCnt3; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    //Array.Reverse(sensorTypeArray);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            case 4:
                                for (int sensorId = 0; sensorId < channelSensorCnt4; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    //Array.Reverse(sensorTypeArray);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            case 5:
                                for (int sensorId = 0; sensorId < channelSensorCnt5; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    //Array.Reverse(sensorTypeArray);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            case 6:
                                for (int sensorId = 0; sensorId < channelSensorCnt6; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    //Array.Reverse(sensorTypeArray);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            case 7:
                                for (int sensorId = 0; sensorId < channelSensorCnt7; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    //Array.Reverse(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            case 8:
                                for (int sensorId = 0; sensorId < channelSensorCnt8; sensorId++)
                                {
                                    //2Byte ushort 传感器n编号(n从1开始)传感器编号由两部分组成：通道号和传感器在通道中的序号。
                                    //bit 8~15：通道号，范围01 - 08 bit 0~7：传感器在通道中的序号，范围01 - 30 例如020F，表示2通道的15号传感器。
                                    byte[] sensorIdArray1 = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16, sensorIdArray1, 0, 2);
                                    //Array.Reverse(sensorIdArray1);
                                    string sensorId1 = BitConverter.ToString(sensorIdArray1);
                                    sensorIdArray1 = null;
                                    //4Byte uint 传感器n原始波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34521,则传输的值为 155634521
                                    byte[] sensorWave0Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 2, sensorWave0Array, 0, 4);
                                    Array.Reverse(sensorWave0Array);
                                    uint sensorWave0 = BitConverter.ToUInt32(sensorWave0Array);
                                    sensorWave0Array = null;
                                    //4Byte uint 传感器n校准波长 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 1556.34512, 则传输的值为 155634512
                                    byte[] sensorWave1Array = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 6, sensorWave1Array, 0, 4);
                                    Array.Reverse(sensorWave1Array);
                                    uint sensorWave1 = BitConverter.ToUInt32(sensorWave1Array);
                                    sensorWave1Array = null;
                                    //2Byte ushort 传感器n类型  0：未定义1：自定义2：温度 3：应变4：压力5：流量 6：位移7：加速度
                                    byte[] sensorTypeArray = new byte[2];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 10, sensorTypeArray, 0, 2);
                                    //Array.Reverse(sensorTypeArray);
                                    ushort sensorType1 = BitConverter.ToUInt16(sensorTypeArray);
                                    sensorTypeArray = null;
                                    //4Byte int 传感器n物理量 传感器的传输值为实际值乘以100000，客户端必须把读取的值除以 100000 才是传感器的实际值，比如传感器的实际值为 12.34512，则传输的值为 1234512
                                    byte[] sensorPhyArray = new byte[4];
                                    Array.Copy(tmpDataArray, beginIndex + channelSensorCnt0 * 16 + 12, sensorPhyArray, 0, 4);
                                    Array.Reverse(sensorPhyArray);
                                    uint sensorPhy = BitConverter.ToUInt32(sensorPhyArray);
                                    sensorPhyArray = null;
                                    beginIndex = beginIndex + 16;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }


        public async Task U3HMqttDataPublic(string jsonString)
        {
            //JsonArray jsonArray = new JsonArray(deviceDataList);
            // 将List转换为JSON字符串
            //string jsonString = JsonConvert.SerializeObject(deviceParamData);

            if (mqttClientManage != null)
            {
                await mqttClientManage.PublishAsync(mqttTopic, jsonString);
            }
        }

        public async Task TCPMqttDataPublic(string[] values)
        {
            List<DeviceParamData> deviceDataList = new List<DeviceParamData>();
            DeviceParamData deviceData = new DeviceParamData();
            deviceData.deviceType = 11;
            string name = values[0];
            deviceData.name = name;
            string id = name.Substring(name.Length - 3, 3);
            deviceData.deviceId = Convert.ToUInt16(id);
            deviceData.value = Convert.ToDouble(values[1]);
            deviceData.value1 = Convert.ToDouble(values[2]);
            deviceData.value2 = Convert.ToDouble(values[3]);
            deviceData.value3 = Convert.ToDouble(values[4]);
            deviceData.timestamp = (int)DateTimeOffset.Now.ToUnixTimeSeconds();//Convert.ToUInt32(values[5]);
            deviceDataList.Add(deviceData);
            //JsonArray jsonArray = new JsonArray(deviceDataList);
            // 将List转换为JSON字符串
            string jsonString = JsonConvert.SerializeObject(deviceDataList);
            if (mqttClientManage != null)
            {
                await mqttClientManage.PublishAsync(mqttTopic, jsonString);
            }
        }

        public async Task TCPv2MqttDataPublic(string[] values)
        {
            List<DeviceParamData> deviceDataList = new List<DeviceParamData>();
            DeviceParamData deviceData = new DeviceParamData();
            deviceData.deviceType = 11;
            string name = values[0];
            deviceData.name = name;
            string id = name.Substring(name.Length - 3, 3);
            deviceData.deviceId = Convert.ToUInt16(id);
            deviceData.value = Convert.ToDouble(values[1]);
            deviceData.value1 = Convert.ToDouble(values[2]);
            deviceData.value2 = Convert.ToDouble(values[3]);
            deviceData.value3 = Convert.ToDouble(values[4]);
            deviceData.value4 = Convert.ToUInt32(values[5]);
            deviceData.value5 = Convert.ToUInt32(values[6]);
            deviceData.value6 = Convert.ToUInt32(values[7]);
            deviceData.value8 = Convert.ToUInt32(values[8]);
            //deviceParamData.value8 = Convert.ToDouble(values[9]);
            deviceData.timestamp = (int)DateTimeOffset.Now.ToUnixTimeSeconds();//Convert.ToUInt32(values[5]);
            deviceDataList.Add(deviceData);
            //JsonArray jsonArray = new JsonArray(deviceDataList);
            // 将List转换为JSON字符串
            string jsonString = JsonConvert.SerializeObject(deviceDataList);
            if (mqttClientManage != null)
            {
                await mqttClientManage.PublishAsync(mqttTopic, jsonString);
            }
        }

        //写入http中转数据
        //键数据添加到窗体底部文本框
        private void AddContent(string ClientIPtxt, string content)
        {
            this.BeginInvoke(new MethodInvoker(delegate
            {
                if (rbAutoLine.Checked && tbMessageData.Text.Length > 0)
                {
                    tbMessageData.AppendText("\r\n");
                }
                tbMessageData.AppendText(ClientIPtxt + content);
                httpReturnValue.TcpServerData = tbMessageData.Text;
            }));
        }

        //点击连接按钮
        private void btnStartListen_Click(object sender, EventArgs e)
        {
            if (isMonitor == false)
            {
                try
                {
                    if (tbTcpServerServerIP.SelectedIndex == 0)
                    {
                        tcpServer = new TcpListener(IPAddress.Any, (int)nudTcpServerServerPort.Value);
                    }
                    else
                    {
                        tcpServer = new TcpListener(IPAddress.Parse(tbTcpServerServerIP.SelectedItem.ToString()), (int)nudTcpServerServerPort.Value);
                    }
                    tcpServer.Start();
                    tcpServer.BeginAcceptTcpClient(new AsyncCallback(Acceptor), tcpServer);
                    //启动监听
                    isMonitor = true;
                    btnStartListen.Text = "关闭监听";

                    //设置控件不可用
                    tbTcpServerServerIP.Enabled = false;
                    nudTcpServerServerPort.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                //关闭监听
                tcpServer.Stop();
                try
                {
                    //循环关闭客户端列表监听
                    foreach (Thread itemthread in tcpClientThreads)
                    {
                        itemthread.Abort();
                    }
                    if (receiveThread != null)
                    {
                        receiveThread.Abort();
                        tbnAutoLoopSend.Text = "循环发送";
                    }
                }
                catch
                {

                }
                isMonitor = false;
                btnStartListen.Text = "启动监听";
                //设置控件可用
                tbTcpServerServerIP.Enabled = true;
                nudTcpServerServerPort.Enabled = true;
            }

        }

        private void Acceptor(IAsyncResult o)
        {
            TcpListener server = o.AsyncState as TcpListener;
            try
            {
                //初始化连接的客户端
                //NetWorks server.EndAcceptTcpClient(o);
                TcpClient jytTcpClient = server.EndAcceptTcpClient(o);
                tcpClients.Add(jytTcpClient);

                dictInfo.Add(((IPEndPoint)jytTcpClient.Client.LocalEndPoint).Port + "->" + jytTcpClient.Client.RemoteEndPoint.ToString());
                server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);//继续监听客户端连接

                lbConnect.Invoke(new MethodInvoker(delegate
                {
                    lbConnect.DataSource = null;
                    lbConnect.DataSource = dictInfo;
                    lbConnect.DisplayMember = "Name";
                }));

                Thread th = new Thread(Recive);
                tcpClientThreads.Add(th);
                th.IsBackground = true;
                th.Start(jytTcpClient);
            }
            catch (ObjectDisposedException ex)
            { //监听被关闭
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void Recive(object client)
        {
            NetworkStream reciveStream = ((TcpClient)client).GetStream();
            //Socket socketSend = o as Socket;
            while (true)
            {
                try
                {
                    NetworkStream nss = ((TcpClient)client).GetStream();
                    byte[] data2 = new byte[102400];//存储客户端发送过来的数据
                    int _length = reciveStream.Read(data2, 0, data2.Length);
                    if (_length > 0)
                    {
                        //string msg = Encoding.Default.GetString(data2, 0, _length);
                        //int length = socketSend.Receive(data2);
                        //string message2 = Encoding.UTF8.GetString(data2, 0, length);
                        //Console.WriteLine("接收到客户端发送的消息" + message2);
                        //AddDataAsync(("从ip：" + ((TcpClient)client).Client.RemoteEndPoint.ToString() + "收到了数据："), System.Text.Encoding.Default.GetBytes(msg));
                        //AddData(((IPEndPoint)((TcpClient)client).Client.LocalEndPoint).Port + "->" + ((TcpClient)client).Client.RemoteEndPoint.ToString()+":" , System.Text.Encoding.Default.GetBytes(msg));
                        //ShowMsg(socketSend.RemoteEndPoint + ":" + str);
                        string clientIp = "从ip：" + ((TcpClient)client).Client.RemoteEndPoint.ToString() + "收到了数据：";
                        AddDataAsync(clientIp, data2);
                        data2 = null;
                    }
                    else
                    {
                        try
                        {
                            if (((TcpClient)client) != null && ((TcpClient)client).Connected)
                            {
                                NetworkStream ns = ((TcpClient)client).GetStream();
                                ns.Close();
                                ((TcpClient)client).Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "错误");
                        }
                    }
                }
                catch
                { }
            }
        }

        private void TcpServerFrom_Load(object sender, EventArgs e)
        {
            if (this.DesignMode == false)
            {
                tbTcpServerServerIP.SelectedIndex = 0;
                IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
                foreach (IPAddress ip in ipHostEntry.AddressList)
                {
                    if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {//筛选IPV4
                        tbTcpServerServerIP.Items.Add(ip.ToString());
                    }
                }
            }
        }

        private void OutTxtClear_Click(object sender, EventArgs e)
        {
            tbMessageData.Text = "";
        }

        private void MS_Add_Click(object sender, EventArgs e)
        {
            AddCmdTxts f2 = new AddCmdTxts("");
            if (f2.ShowDialog() == DialogResult.OK)
            {

                int index = this.dgvCMDColumns.Rows.Add();
                this.dgvCMDColumns.Rows[index].Cells[0].Value = false;
                this.dgvCMDColumns.Rows[index].Cells[1].Value = f2.TextBoxValue;
                this.dgvCMDColumns.Rows[index].Cells[2].Value = "发送";
                f2.Close();
            }
        }

        private void MS_Edit_Click(object sender, EventArgs e)
        {
            try
            {
                AddCmdTxts f2 = new AddCmdTxts(dgvCMDColumns.Rows[dgvCMDColumns.CurrentRow.Index].Cells[1].Value.ToString());
                if (f2.ShowDialog() == DialogResult.OK)
                {
                    dgvCMDColumns.Rows[dgvCMDColumns.CurrentRow.Index].Cells[1].Value = f2.TextBoxValue;

                    f2.Close();
                }
            }
            catch
            {

            }
        }

        private void MS_Delete_Click(object sender, EventArgs e)
        {
            this.dgvCMDColumns.ClearSelection();
            foreach (DataGridViewRow rowitme in this.dgvCMDColumns.Rows)
            {

                if (rowitme.Cells[0].Value.ToString() == "True")
                {
                    this.dgvCMDColumns.Rows.Remove(rowitme);

                }
            }
            if (dgvCMDColumns.CurrentRow.Index == dgvCMDColumns.Rows.Count - 1)
            {
                if (dgvCMDColumns.Rows[dgvCMDColumns.CurrentRow.Index].Cells[0].Value.ToString() == "True")
                {
                    this.dgvCMDColumns.Rows.Remove(dgvCMDColumns.Rows[dgvCMDColumns.CurrentRow.Index]);

                }
            }
        }

        private void tbnAutoLoopSend_Click(object sender, EventArgs e)
        {
            if (tbnAutoLoopSend.Text == "停止发送")
            {
                tbnAutoLoopSend.Text = "循环发送";
                receiveThread.Abort();
                //timer1.Enabled = false;
                return;
            }
            if (isMonitor)
            {
                receiveThread = new Thread(CircularTransmission);//创建线程

                receiveThread.Start();//启动线程

                tbnAutoLoopSend.Text = "停止发送";
            }
            else
            {
                receiveThread.Abort();
                tbnAutoLoopSend.Text = "循环发送";
                MessageBox.Show("监听未打开", "错误");
            }
        }

        //循环发送
        private void CircularTransmission()
        {
            try
            {
                if (isMonitor)
                {
                    while (true)
                    {

                        foreach (DataGridViewRow rowitme in this.dgvCMDColumns.Rows)
                        {

                            if (rowitme.Cells[0].Value.ToString() == "True")
                            {
                                Thread.Sleep(int.Parse(nudIntervalDelay.Value.ToString()));

                                byte[] _jytbyte = Transformation(rowitme.Cells[1].Value.ToString());// System.Text.Encoding.Default.GetBytes(rowitme.Cells[1].Value.ToString());
                                tcpClients[lbConnect.SelectedIndex].GetStream().Write(_jytbyte, 0, _jytbyte.Length);
                            }
                        }
                    }
                }
                else
                {
                    receiveThread.Abort();
                    tbnAutoLoopSend.Text = "循环发送";
                }
            }
            catch
            {
                receiveThread.Abort();
                tbnAutoLoopSend.Text = "循环发送";
                MessageBox.Show("发送失败", "错误");
            }
        }

        private void btnSendCommand_Click(object sender, EventArgs e)
        {
            if (this.tbCmdTxt.Text != "")
                btnSendCommandOut(this.tbCmdTxt.Text);
        }
        //发送单条
        public void btnSendCommandOut(string data)
        {
            try
            {
                byte[] _jytbyte = Transformation(data);// System.Text.Encoding.Default.GetBytes(data);
                tcpClients[lbConnect.SelectedIndex].GetStream().Write(_jytbyte, 0, _jytbyte.Length);
            }
            catch
            {
                MessageBox.Show("发送失败", "错误");
            }
        }

        private void dgvCMDColumns_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string buttonText = this.dgvCMDColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            if (buttonText == "发送")
            {

                btnSendCommandOut(this.dgvCMDColumns.Rows[e.RowIndex].Cells[1].Value.ToString());

            }
        }

        //通过这个判断 选择的编码格式 下面这四个方法 （有更简单的方法实现这个，--->--->--->--->--->--->--->--->--->--->--->--->--->--->--->--->自行脑补吧）
        int CodingType = 0;
        private void rbHex_CheckedChanged(object sender, EventArgs e)
        {
            if (rbHex.Checked)
            {
                CodingType = 0;
            }
        }

        private void rbASCII_CheckedChanged(object sender, EventArgs e)
        {
            if (rbASCII.Checked)
            {
                CodingType = 1;
            }
        }

        private void rbUTF8_CheckedChanged(object sender, EventArgs e)
        {
            if (rbUTF8.Checked)
            {
                CodingType = 3;
            }
        }

        private void rbGB2312_CheckedChanged(object sender, EventArgs e)
        {
            if (rbGB2312.Checked)
            {
                CodingType = 2;
            }
        }

        private void DisconnectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lbConnect.Items.Count; i++)
            {
                if (lbConnect.SelectedIndex != -1)
                {
                    //右键断开时，释放客户端所有关系
                    try { dictInfo.RemoveAt(i); } catch { }
                    try { tcpClients.RemoveAt(i); } catch { }
                    try { tcpClientThreads[i].Abort(); } catch { }
                    try { tcpClientThreads.RemoveAt(i); } catch { }
                }
            }
            //重新绑定列表
            lbConnect.Invoke(new MethodInvoker(delegate
            {
                lbConnect.DataSource = null;
                lbConnect.DataSource = dictInfo;
                lbConnect.DisplayMember = "Name";
            }));
        }
        //处理发送数据 时 将内容转换为 选择jytCodingtype的编码格式数据
        private byte[] Transformation(string textdata)
        {
            byte[] data = null;
            switch (CodingType)
            {
                case 0:
                    string[] HexStr = textdata.Trim().Split(' ');
                    data = new byte[HexStr.Length];
                    for (int i = 0; i < HexStr.Length; i++)
                    {
                        data[i] = (byte)(Convert.ToInt32(HexStr[i], 16));
                    }
                    break;
                case 1:
                    data = new ASCIIEncoding().GetBytes(textdata);
                    break;
                case 2:
                    data = new UTF8Encoding().GetBytes(textdata);
                    break;
                case 3:
                    data = Encoding.GetEncoding("GB2312").GetBytes(textdata);
                    break;
            }
            return data;
        }

        public async Task SaveDataToMysql(string[] values, long nowTime, int sensorType)
        {
            string name = values[0];
            string id = name.Substring(name.Length - 3, 3);
            int deviceId = Convert.ToUInt16(id);
            try
            {
                MainForm.db.ExecuteSql(
                    MainForm.db.Insert("raster_acoustic_emission")
                    .SetColumn("collect_time", nowTime)
                    .SetColumn("collect_datetime", (new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)nowTime).ToLocalTime()).ToString("yyyy-MM-dd HH:mm:ss"))
                    .SetColumn("sensor_id", deviceId)
                    .SetColumn("sensor_name", name)
                    .SetColumn("sensor_type", sensorType)
                    .SetColumn("count_value1", values[1])
                    .SetColumn("count_value2", values[2])
                    .SetColumn("count_value3", values[3])
                    .SetColumn("count_value4", values[4])
                    .SetColumn("count_value5", values[5])
                    .SetColumn("count_value6", values[6])
                    .SetColumn("count_value7", values[7])
                    .SetColumn("count_value8", values[8])
                    .ToSql());
            }
            catch (Exception e)
            {

            }
        }
        /**
         *处理发送字符 十六进制    ASCII     UTF8    GB2312
         * */
        /*
         switch (_EncodeType)
        {
            case EnumType.DataEncode.Hex:
                string[] HexStr = this.Text.Trim().Split(' ');
                data = new byte[HexStr.Length];
                for (int i = 0; i < HexStr.Length; i++)
                {
                    data[i] = (byte)(Convert.ToInt32(HexStr[i], 16));
                }
                break;
            case EnumType.DataEncode.ASCII:
                data = new ASCIIEncoding().GetBytes(this.Text);
                break;
            case EnumType.DataEncode.UTF8:
                data = new UTF8Encoding().GetBytes(this.Text);
                break;
            case EnumType.DataEncode.GB2312:
                data = Encoding.GetEncoding("GB2312").GetBytes(this.Text);
                break;
        }
        switch (value)
        {
            case EnumType.DataEncode.Hex:
                if (this.Text.Length > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < data.Length; i++)
                    {
                        sb.AppendFormat("{0:x2} ", data[i]);
                    }
                    this.Text = sb.ToString().Trim().ToUpper();
                }
                break;
            case EnumType.DataEncode.ASCII:
                this.Text = new ASCIIEncoding().GetString(data);
                break;
            case EnumType.DataEncode.UTF8:
                this.Text = new UTF8Encoding().GetString(data);
                break;
            case EnumType.DataEncode.GB2312:
                this.Text = Encoding.GetEncoding("GB2312").GetString(data);
                break;
        }
         */
    }
}

public class DeviceParamData
{
    //设备ID
    public int deviceId { get; set; }
    //设备类型
    public int deviceType { get; set; }
    //设备名称
    public string name { get; set; }
    //上传时间
    public int timestamp { get; set; }
    //纳秒
    public int nanoSeconds { get; set; }
    //数据类型 11参数 10波形
    public int dataType { get; set; }
    //AMP，幅度，单位为dB
    public double value { get; set; }
    //Power，能量，单位为 KpJ
    public double value1 { get; set; }
    //RMS，有效电压，单位为mV 
    public double value2 { get; set; }
    //ASL，平均信号电平值，单位为 dB
    public double value3 { get; set; }
    //上升时间，单位为 us
    public uint value4 { get; set; }
    //上升计数，单位为个
    public uint value5 { get; set; }
    //波形点数，单位为个
    public uint value6 { get; set; }
    //持续时间，单位为 us
    public uint value7 { get; set; }
    //振铃计数，单位为个
    public uint value8 { get; set; }
    //峰值频率，单位 KHz
    public uint value9 { get; set; }
    //质心频率，单位 KHz
    public uint value10 { get; set; }
    //局部功率谱 1 占比
    public uint value11 { get; set; }
    //局部功率谱 2 占比
    public uint value12 { get; set; }
    //局部功率谱 3 占比
    public uint value13 { get; set; }
    //局部功率谱 4 占比
    public uint value14 { get; set; }
    //局部功率谱 5 占比
    public uint value15 { get; set; }

    public override string ToString()
    {
        return deviceId + ", " + deviceType + ", " + name + ", " + timestamp + ", " + nanoSeconds + ", " + dataType + "," + value + ", " + value1 + ", " + value2 + ", " + value3 + ", " + value4 + ", " + value5 + ", " + value6 + ", " + value8 + ", " + value9 + ", " + value10 + ", " + value11 + ", " + value12 + ", " + value13 + ", " + value14 + ", " + value15;
    }
}

public class DeviceWaveData
{
    //设备ID
    public int deviceId { get; set; }
    //设备类型
    public int deviceType { get; set; }
    //设备名称
    public string name { get; set; }
    //上传时间
    public int timestamp { get; set; }
    //纳秒
    public int nanoSeconds { get; set; }
    //数据类型 11参数 10波形
    public int dataType { get; set; }
    //前采点数，单位为个
    public uint value { get; set; }
    //波形点数，单位为个
    public uint value1 { get; set; }
    //采样速率，单位为 K/S
    public uint value2 { get; set; }
    //前放增益，单位为倍
    public uint value3 { get; set; }
    //电路放大倍数，单位为倍
    public double value4 { get; set; }
    //波形数据 n 一个波形数据为 2 字节，即此处有 2n 个波形点数的数据，总大小为 2n 字节
    public double[] value5 { get; set; }

    public override string ToString()
    {
        return deviceId + ", " + deviceType + ", " + name + ", " + timestamp + ", " + nanoSeconds + ", " + dataType + ", " + value + ", " + value1 + ", " + value2 + ", " + value3 + ", " + value4 + ", " + value5;
    }
}